const express = require('express')
const Router = express.Router()
const model = require('./model')
const User = model.getModel('user')
const Chat = model.getModel('chat')
const utils = require('utility')
const _filter ={'pwd':0,'__v':0}
Router.get('/list',function(req,res){
  //删除数据库
  // User.remove({},function(err,doc){})
  const { type } = req.query 
  User.find({type},function(err,doc){
    return res.json({code:0,data:doc})
  })
})
Router.get('/getmsglist',function(req,res){
  const user = req.cookies.user
  // {'$or':[{from:user,to:user}]}
  Chat.find({},function(err,doc){
    if(!err){
      return res.json({code:0,msgs:doc})
    }
  })
})

Router.post('/register',function(req,res){
  console.log(req.body)
  const {user, pwd, type} = req.body
  User.findOne({user},function(err,doc){
    if(doc){
      return res.json({code:1,msg:'用户名重复'})
    }
    const userModel = new User({user,type,pwd:md5Pwd(pwd)})
    userModel.save(function(e,d){
      if(e){
        console.log(e)
        return res.json({code:1,msg:'后端出错'})
      }
      const {user,type,_id} = d
      res.cookie('userid',d._id)
      return res.json({code:0,data:{user, type, _id}})
    })
  })
})
Router.post('/update',function(req,res){
  const userid = req.cookies.userid
	if (!userid) {
		return json.dumps({code:1})
	}
  const body = req.body
  console.log(body)
	User.findByIdAndUpdate(userid,body,function(err,doc){
		const data = Object.assign({},{
			user:doc.user,
			type:doc.type
		},body)
		return res.json({code:0,data})
	})
})
Router.post('/login',function(req,res){
  const {user, pwd} = req.body
  User.findOne({user,pwd:md5Pwd(pwd)},_filter,function(err,doc){
    if(!doc){
      return res.json({code:1,msg:'用户名密码不正确'})
    }
    res.cookie('userid',doc._id)
    return res.json({code:0,data:doc})
  })
  
})

Router.get('/info',function(req,res){
  const {userid} = req.cookies
  if(!userid){
    return res.json({code:1})
  }
  User.findOne({_id:userid}, _filter, function(err,doc){
    if(err){
      return res.json({code:1,msg:'后端出错了'})
    }
    if(doc){
      return res.json({code:0,data:doc})
    }
  })
})

function md5Pwd(pwd){
  const salt = 'nzdmwxjzmwxjxmwtm@wywscxj'
  return utils.md5(utils.md5(pwd+salt))
}
module.exports = Router